#include "Data.h"

namespace KVConfig {

    bool Data::has(const std::string &key) const
    {
        KVMap::const_iterator iter = m_data.find(key);
        return iter != m_data.end();
    }

    std::string Data::get(const std::string &key) const
    {
        KVMap::const_iterator iter = m_data.find(key);
        if (iter != m_data.end()) {
            return iter->second;
        }
        return std::string();
    }

    void Data::set(const std::string &key, const std::string &value)
    {
        m_data.insert(std::make_pair(key, value));
    }

    void Data::clear()
    {
        m_data.clear();
    }

    std::string Data::operator[](const std::string &key) const
    {
        return get(key);
    }

    bool Data::get(const std::string &key, std::string &value) const
    {
        KVMap::const_iterator iter = m_data.find(key);
        if (iter != m_data.end()) {
            value = iter->second;
            return true;
        }  else {
            return false;
        }
    }

    bool Data::get(const std::string &key, bool &value) const
    {
        KVMap::const_iterator iter = m_data.find(key);
        if (iter != m_data.end()) {
            std::string strValue = iter->second;
            if (strValue == "Y" || strValue == "y" 
                || strValue == "yes" || strValue == "Yes"
                || strValue == "true" || strValue == "True"
                || strValue == "1")
                value = true;
            else if (strValue == "N" || strValue == "n" 
                || strValue == "no" || strValue == "No"
                || strValue == "false" || strValue == "False"
                || strValue == "0")
                value = true;
            else
                return false;
            return true;
        } else {
            return false;
        }
    }
}
